package link.jfire.jnet.common;

import java.util.Queue;
import java.util.concurrent.LinkedTransferQueue;
import link.jfire.jnet.client.AioClient;
import link.jfire.jnet.client.ClientInternalResult;
import link.jfire.jnet.server.storage.ServerInternalResult;
import link.jfire.jnet.server.transfer.server.ServerChannelInfo;

public class InternalResultContainer
{
    private static Queue<ServerInternalResult> queue       = new LinkedTransferQueue<>();
    private static Queue<ClientInternalResult> clientQueue = new LinkedTransferQueue<>();
                                                           
    public static ClientInternalResult buildClient(Object data, AioClient aioClient, int index)
    {
        ClientInternalResult result = clientQueue.poll();
        result = result == null ? new ClientInternalResult() : result;
        result.init(aioClient, data, index);
        return result;
    }
    
    public static ServerInternalResult buildServer(Object data, ServerChannelInfo channelInfo, int index)
    {
        ServerInternalResult one = queue.poll();
        one = one == null ? new ServerInternalResult() : one;
        one.init(data, channelInfo, index);
        return one;
    }
    
    public static void recycleServer(ServerInternalResult result)
    {
        result.clear();
        queue.offer(result);
    }
    
    public static void recycleClient(ClientInternalResult result)
    {
        result.clear();
        clientQueue.offer(result);
    }
}
